﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;

using ConfigDashboard.Models;

namespace ConfigDashboard.Controllers
{
    public class HomeController : Controller
    {
        private ConfigSettingsEntities db = new ConfigSettingsEntities();

        //
        // GET: /Home/

        public ActionResult Index()
        {
            ValuesViewModel viewModel = new ValuesViewModel();
            return View(viewModel);
        }


        //
        // GET: /Home/Create/5

        public ActionResult Create(int id = 0, int env = 0)
        {
            ConfigSettingValue configsettingvalue = new ConfigSettingValue();
            configsettingvalue.EnvironmentID = env;
            configsettingvalue.SettingID = id;

            ValueViewModel viewModel = new ValueViewModel()
                {
                    Value = configsettingvalue,
                    Environment = db.ConfigEnvironments.Single(e => e.EnvironmentID == env),
                    SettingName = db.ConfigSettingNames.Single(n => n.SettingID == id)
                };

            return View(viewModel);
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create([Bind(Prefix = "Value")]ConfigSettingValue configsettingvalue)
        {
            if (ModelState.IsValid)
            {
                db.ConfigSettingValues.AddObject(configsettingvalue);
                db.SaveChanges();
                //return RedirectToAction("Index");
                return Redirect(Url.RouteUrl(new { controller = "Home", action = "Index" }) + "#" + configsettingvalue.ConfigSettingName.SettingName); 
            }

            ValueViewModel viewModel = new ValueViewModel()
                {
                    Value = configsettingvalue,
                    Environment = configsettingvalue.ConfigEnvironment,
                    SettingName = configsettingvalue.ConfigSettingName
                };

            return View(viewModel);
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id = 0, int env = 0)
        {
            ConfigSettingValue configsettingvalue = db.ConfigSettingValues.Single(c => c.SettingID == id && c.EnvironmentID == env);
            if (configsettingvalue == null)
            {
                return HttpNotFound();
            }

            ValueViewModel viewModel = new ValueViewModel()
                {
                    Value = configsettingvalue,
                    Environment = configsettingvalue.ConfigEnvironment,
                    SettingName = configsettingvalue.ConfigSettingName
                };

            return View(viewModel);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit([Bind(Prefix="Value")]ConfigSettingValue configsettingvalue)
        {
            if (ModelState.IsValid)
            {
                db.ConfigSettingValues.Attach(configsettingvalue);
                db.ObjectStateManager.ChangeObjectState(configsettingvalue, EntityState.Modified);
                db.SaveChanges();
                //return RedirectToAction("Index");
                return Redirect(Url.RouteUrl(new { controller = "Home", action = "Index" }) + "#" + configsettingvalue.ConfigSettingName.SettingName);
            }

            ValueViewModel viewModel = new ValueViewModel()
                {
                    Value = configsettingvalue,
                    Environment = configsettingvalue.ConfigEnvironment,
                    SettingName = configsettingvalue.ConfigSettingName
                };

            return View(viewModel);
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id = 0, int env = 0)
        {
            ConfigSettingValue configsettingvalue = db.ConfigSettingValues.Single(c => c.SettingID == id && c.EnvironmentID == env);
            if (configsettingvalue == null)
            {
                return HttpNotFound();
            }

            ValueViewModel viewModel = new ValueViewModel()
                {
                    Value = configsettingvalue,
                    Environment = configsettingvalue.ConfigEnvironment,
                    SettingName = configsettingvalue.ConfigSettingName
                };

            return View(viewModel);
        }

        //
        // POST: /Home/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id, int env = 0)
        {
            ConfigSettingValue configsettingvalue = db.ConfigSettingValues.Single(c => c.SettingID == id && c.EnvironmentID == env);
            string settingName = configsettingvalue.ConfigSettingName.SettingName;
            db.ConfigSettingValues.DeleteObject(configsettingvalue);
            db.SaveChanges();
            //return RedirectToAction("Index");
            return Redirect(Url.RouteUrl(new { controller = "Home", action = "Index" }) + "#" + settingName);
        }


        /// <summary>
        /// Releases unmanaged resources and optionally releases managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
